package com.cy.ppj.web.admin.user;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cy.ppj.comm.constant.Constants;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.user.SalesAddAO;
import com.cy.ppj.model.domian.account.DealRecord;
import com.cy.ppj.model.dto.user.UserAddDTO;
import com.cy.ppj.model.enums.UserStatus;
import com.cy.ppj.model.enums.UserType;
import com.cy.ppj.model.query.ChannelUserBySalesAdminQuery;
import com.cy.ppj.model.query.DealRecordAdminQuery;
import com.cy.ppj.model.query.OrderAdminByTypeQuery;
import com.cy.ppj.model.query.UserAdminQuery;
import com.cy.ppj.model.query.account.TeamMemberQuery;
import com.cy.ppj.model.vo.ChannelUserBySalesAdminVO;
import com.cy.ppj.model.vo.OrderAdminByTypeVO;
import com.cy.ppj.model.vo.SalesUserAdminVO;
import com.cy.ppj.model.vo.UserDetailAdminVO;
import com.cy.ppj.model.vo.admin.dealrecord.DealRecordAdminVO;
import com.cy.ppj.model.vo.admin.teamcommission.TeamMemberAdminVO;
import com.cy.ppj.service.account.TeamCommissionService;
import com.cy.ppj.service.dealrecord.DealRecordService;
import com.cy.ppj.service.order.OrderService;
import com.cy.ppj.service.user.UserService;
import com.cy.ppj.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 后台-业务员管理
 * @author: tanyilin
 * @create: 2018年8月1日 上午11:49:31
 */
@Slf4j
@RestController
@RequestMapping("/admin/user/sales")
@Api(tags = "后台-业务员管理")
public class AdminSalesUserController extends BaseController{

	@Autowired
	private UserService userService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private DealRecordService dealRecordService;
	@Autowired
	private TeamCommissionService teamCommissionService;
	
	@ApiOperation("业务员列表")
	@GetMapping("/list")
	public PageBean<SalesUserAdminVO> list(UserAdminQuery userAdminQuery) {
		return userService.querySalesUserList(userAdminQuery);
	}
	
	@ApiOperation("业务员信息详情")
	@GetMapping("/detail")
	public Result<UserDetailAdminVO> detail(@ApiParam(value = "用户ID", example = "0") @RequestParam Long userId) {
		UserDetailAdminVO userDetailAdminVO = userService.queryUserDetailAdmin(userId);
		return ok("查询成功", userDetailAdminVO);
	}
	
	@ApiOperation("业务员资金记录")
	@GetMapping("/deal/records")
	public PageBean<DealRecordAdminVO> realRecords(DealRecordAdminQuery query) {
		PageBean<DealRecordAdminVO> pageBean = dealRecordService.queryDealRecordAdmin(query);
		return pageBean;
	}
	
	@ApiOperation("解绑银行卡")
	@PostMapping("/unbind/bank")
	public Result unbindBank(@ApiParam(value = "用户ID", example = "0", required = true) @RequestParam Long userId) {
		userService.unBindBank(userId);
		return ok();
	}
	
	@ApiOperation("渠道员列表")
	@GetMapping("/my/channel/list")
	public PageBean<ChannelUserBySalesAdminVO> channelUsers(ChannelUserBySalesAdminQuery channelUserBySalesAdminQuery) {
		return userService.queryChannelUserBySalesAdmin(channelUserBySalesAdminQuery);
	}
	
	@ApiOperation("渠道-订单明细")
	@GetMapping("/mychannel/order/list")
	public PageBean<OrderAdminByTypeVO> mychannelOrderList(OrderAdminByTypeQuery query) {
		query.setType(2);
		return orderService.queryOrderAdminByType(query);
	}
	
	@ApiOperation("自己-订单明细")
	@GetMapping("/my/order/list")
	public PageBean<OrderAdminByTypeVO> myOrderList(OrderAdminByTypeQuery query) {
		query.setType(1);
		return orderService.queryOrderAdminByType(query);
	}
	
	@ApiOperation("更改用户状态")
	@PostMapping("/update/status")
	public Result updateStatus(@ApiParam(value = "用户ID", example = "0", required = true) @RequestParam Long userId,
			@ApiParam(value = "用户状态", required = true) @RequestParam UserStatus userStatus) {
		userService.updateUserStatus(userId, userStatus);
		return ok();
	}
	
	@ApiOperation("添加业务员")
	@PostMapping("/add")
	public Result addSales(@Validated SalesAddAO salesAddAO, BindingResult bindingResult) {
		this.checkError(bindingResult);
		UserAddDTO userAddDTO = new UserAddDTO();
        BeanUtils.copyProperties(salesAddAO, userAddDTO);
        userAddDTO.setUserType(UserType.SALES);
        userAddDTO.setPassword(Constants.USER_INIT_PASSWORD);
        userService.insertUser(userAddDTO);
		return ok();
	}
	
	@ApiOperation("邀请人数列表")
	@GetMapping("/my/team/members")
	public PageBean<TeamMemberAdminVO> myTeamMembers(TeamMemberQuery query) {
		PageBean<TeamMemberAdminVO> pageBean = teamCommissionService.selectTeamMemberAdmin(query);
		return pageBean;
	}
	
	
	
}
